{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ea3206f",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mFailed to start the Kernel. \n",
      "\u001b[1;31mUnable to start Kernel 'jyx-py39 (Python 3.9.23)' due to a timeout waiting for the ports to get used. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from utils import data_utils\n",
    "import config.config_ml as config \n",
    "from models.classification.ml_model import RFClassificationModel\n",
    "\n",
    "# ===读取数据===\n",
    "df = pd.read_pickle(r'data/imputed/imputed_dataset_all.pkl')\n",
    "df[config.LABEL_COL] = data_utils.extract_binary_label(df, config.LABEL_COL)\n",
    "print(\"df.shape:\", df.shape)\n",
    "\n",
    "# 特征矩阵和标签\n",
    "X = df.drop(columns=[config.LABEL_COL, 'subject_id', 'impute_id'])\n",
    "y = df[config.LABEL_COL]\n",
    "\n",
    "# ===选择模型===\n",
    "model = RFClassificationModel(random_state=42)\n",
    "model.load(f\"results/classification/rf/best/best_model_rf.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f76a5aee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import shap\n",
    "\n",
    "# === 初始化 SHAP ===\n",
    "explainer = shap.Explainer(model, X)\n",
    "shap_values = explainer(X)\n",
    "\n",
    "# === 全局可视化特征重要性 ===\n",
    "shap.summary_plot(shap_values, X, plot_type=\"bar\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "jyx-py39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
